package com.tomato.tv.adapter

import android.animation.ArgbEvaluator
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.tomato.tv.R
import com.tomato.tv.app.App
import com.tomato.tv.entity.Channel
import com.tomato.tv.glide.GlideApp


class HistoryItemAdapter(layoutResId: Int = R.layout.item_history) :
    BaseQuickAdapter<Channel, BaseViewHolder>(layoutResId), View.OnFocusChangeListener {

    override fun convert(holder: BaseViewHolder, item: Channel) {
        val ivImage = holder.getView<ImageView>(R.id.iv_image)
        GlideApp.with(App.instance)
            .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F01%2F210924111121L18-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656641569&t=f5a2edf7c934c9777479cf50e40b77fc")
            .apply(
                RequestOptions().transform(CenterCrop(),RoundedCorners(10))
            )
            .into(ivImage)

        val container = holder.getView<ConstraintLayout>(R.id.container)
        container.onFocusChangeListener = this

        val tvName = holder.getView<TextView>(R.id.tv_name)
        tvName.text = item.name
    }

    override fun onFocusChange(v: View?, hasFocus: Boolean) {
        val scaleX = if (hasFocus) 1.1f else 1.0F
        val scaleY = if (hasFocus) 1.1f else 1.0F
        v?.let {
            ViewCompat.animate(it)
                .scaleX(scaleX)
                .scaleY(scaleY)
                .start()
        }
    }

}